package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.domain.ZzWordBody;
import com.zzedu.domain.ZzWordHead;
import com.zzedu.dto.ZzWordHeadDTO;
import com.zzedu.framework.transaction.ITransactionHandlerService;
import com.zzedu.integration.fileread.FileReadFactoryService;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.mapper.ZzWordHeadMapper;
import com.zzedu.service.IZzUploadFileService;
import com.zzedu.service.IZzWordBodyService;
import com.zzedu.service.IZzWordHeadService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * 单词统计Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-03-16
 */
@Service
public class ZzWordHeadServiceImpl extends ServiceImpl<ZzWordHeadMapper, ZzWordHead> implements IZzWordHeadService {
    Logger log = LoggerFactory.getLogger(ZzWordHeadServiceImpl.class);

    @Autowired
    IOssClientService ossClientService;

    @Autowired
    IZzUploadFileService zzUploadFileService;

    @Autowired
    ITransactionHandlerService transactionHandlerService;

    @Autowired
    private IZzWordBodyService wordBodyService;

    @Autowired
    private List<FileReadFactoryService> fileReadFactoryServices;

    @Override
    public LambdaQueryWrapper<ZzWordHead> buildQuery(ZzWordHeadDTO zzWordHead) {
        LambdaQueryWrapper<ZzWordHead> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzWordHead.getFileName()), ZzWordHead::getFileName, zzWordHead.getFileName());

        lambdaQueryWrapper.orderByDesc(ZzWordHead::getId);
        return lambdaQueryWrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void doFile(MultipartFile file) throws Exception{
        List<String> allWords = Lists.newArrayList();
        for (FileReadFactoryService factoryService : fileReadFactoryServices) {
            if (factoryService.isRead(file.getOriginalFilename())) {
                List<String> dataWords = factoryService.readFile(file.getInputStream());
                if (CollectionUtils.isNotEmpty(dataWords)) {
                    allWords.addAll(dataWords);
                }
            }
        }
        transactionHandlerService.runInTransaction(() -> {
            LinkedHashMap<String, Long> countMap = Maps.newLinkedHashMap();
            Set<String> words = Sets.newLinkedHashSet();
            for (String word : allWords) {
                String key = word.toLowerCase();
                Long count = countMap.getOrDefault(key, 0L);
                count++;
                countMap.put(key, count);
                words.add(key);
            }
            List<ZzWordBody> wordItemList = Lists.newArrayList();
            ZzWordHead wordContent = new ZzWordHead();
            wordContent.setFileName(file.getOriginalFilename());
            super.save(wordContent);
            for (String key : words) {
                ZzWordBody wordItem = new ZzWordBody();
                wordItem.setWordContent(key);
                Long count = countMap.get(key);
                wordItem.setWordCount(count);
                wordItem.setFileId(wordContent.getId());
                wordItemList.add(wordItem);
            }
            wordBodyService.saveBatch(wordItemList);
            return true;
        });
    }
}